package cn.sytton.taffe.web.sys.controller;

import cn.sytton.taffe.excel.annotation.ExportExcel;
import cn.sytton.taffe.web.sys.entity.Auth;
import cn.sytton.taffe.web.sys.entity.DeptUserEntity;
import cn.sytton.taffe.web.sys.service.IDeptMenuService;
import com.github.pagehelper.PageInfo;
import cn.sytton.taffe.framework.base.controller.BaseController;
import cn.sytton.taffe.framework.base.entity.PageParam;
import cn.sytton.taffe.web.sys.entity.DeptEntity;
import cn.sytton.taffe.web.sys.service.IDeptService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * 部门控制层
 *
 * @author skyrock
 */
//@Api(tags = "部门")
@Tag(name = "部门")
@RestController
@RequiredArgsConstructor
@RequestMapping("/sys/dept")
public class DeptController extends BaseController {

    private final IDeptService deptService;
    private final IDeptMenuService deptMenuService;

    @Operation(summary = "查询分页部门列表")
    @Parameter(name = "pp", description = "分页参数对象", in = ParameterIn.QUERY, required = true)
    @ExportExcel
    @GetMapping
    public PageInfo<DeptEntity> findListPage(PageParam pp) {
        return deptService.findListPageOfXml(pp);
    }

    @Operation(summary = "查询单个部门")
    @Parameter(name = "id", description = "部门ID", in = ParameterIn.PATH, required = true)
    @GetMapping("/{id}")
    public DeptEntity findById(@PathVariable String id) {
        return deptService.findById(id);
    }

    @Operation(summary = "查询多个部门", description = "body: 主键ID数组")
    @PostMapping("/ids")
    public List<DeptEntity> findByIds(@RequestBody String[] ids) {
        return deptService.listByIds(Arrays.asList(ids));
    }

    @Operation(summary = "保存部门")
    @PostMapping
    public void save(@RequestBody @Validated DeptEntity entity) {
        deptService.createOrUpdate(entity);
    }

    @Operation(summary = "删除部门")
    @PostMapping("/delete")
    public void delete(@RequestBody String[] ids) {
        deptService.delete(ids);
    }

    @Operation(summary = "查询部门树")
    @GetMapping("/tree")
    public List<DeptEntity> findTree() {
        return deptService.findTree();
    }

    @Operation(summary = "查询分页部门成员列表")
    @Parameter(name = "pp", description = "分页参数对象", in = ParameterIn.PATH, required = true)
    @ExportExcel
    @GetMapping("/user")
    public PageInfo<DeptUserEntity> findUserListPage(PageParam pp) {
        return deptService.findUserListPage(pp);
    }

    @Operation(summary = "添加成员")
    @Parameter(name = "users", description = "用户ID数组", required = true)
    @PostMapping("/user/add")
    public void addDeptUser(@RequestBody DeptUserEntity users) {
        deptService.addUser(users);
    }

    @Operation(summary = "移除成员")
    @Parameter(name = "users", description = "用户ID数组", required = true)
    @PostMapping("/user/remove")
    public void removeDeptUser(@RequestBody DeptUserEntity users) {
        deptService.removeUser(users);
    }

    @Operation(summary = "更新成员序号")
    @Parameter(name = "user", description = "用户", required = true)
    @PostMapping("/user/seq/update")
    public void updateUserSeq(@RequestBody DeptUserEntity user) {
        deptService.updateUserSeq(user);
    }

    @Operation(summary = "刷新成员序号")
    @Parameter(name = "deptUser", description = "部门成员对象", required = true)
    @PostMapping("/user/seq/refresh")
    public void refreshUserSeq(@RequestBody DeptUserEntity deptUser) {
        deptService.refreshUserSeq(deptUser.getDeptId());
    }

    @Operation(summary = "获取部门的授权菜单ID及功能ID")
    @Parameter(name = "deptId", description = "部门ID", in = ParameterIn.QUERY, required = true)
    @GetMapping("/menu")
    public Map<String, Object> findMenu(@RequestParam(required = false) String deptId) {
        return deptMenuService.findMenu(deptId);
    }

    @Operation(summary = "授权菜单及权限")
    @Parameter(name = "auth", description = "授权对象", required = true)
    @PostMapping("/menu/auth")
    public void authMenu(@RequestBody Auth auth) {
        deptMenuService.authMenu(auth);
    }
}
